<?php

namespace App\Admin\Forms;

use App\Models\Message;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;

class CheckComment extends Form implements LazyRenderable
{
    use LazyWidget;

    // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        $model = $this->payload['model'];

        // 表单参数
        $status = $input['status'] ?? null;

        if (!$id) {
            return $this->response()->error('参数错误');
        }

        $info = $model::query()->find($id);

        if (!$info) {
            return $this->response()->error('数据不存在');
        }

        $info->update(['status' => $status]);
        Message::insert([
            'title' => '审核通知',
            'type' => 2,
            'admin_id' => 0,
            'member_id' => $info->member_id,
            'content' => $status == 1 ? '您的评论已经审核通过' : '您的评论审核不通过',
            'created_at' => date('Y-m-d H:i:s')
        ]);

        return $this->response()->success('操作成功')->refresh();
    }

    public function form()
    {
        // 获取外部传递参数
        //$id = $this->payload['id'] ?? null;
        $payload = $this->payload;
        $this->display('nickname', '昵称')->with(function ($value) use ($payload) {
            return $payload['nickname'];
        });
        $this->display('content', '评论内容')->with(function ($value) use ($payload) {
            return $payload['content'];
        });
        $this->radio('status','审核状态')->options([1 => '通过', 2 => '驳回'])->default(1);
    }
}
